package com.xinyues.gamemanager.service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xinyues.gamemanager.common.StringUtil;
import com.xinyues.gamemanager.dao.GmRoleRepository;
import com.xinyues.gamemanager.dao.entity.GmMenu;
import com.xinyues.gamemanager.dao.entity.GmPermission;
import com.xinyues.gamemanager.dao.entity.GmRole;
import com.xinyues.gamemanager.dao.entity.model.MenuNode;
import com.xinyues.gamemanager.dao.entity.model.Permission;
import com.xinyues.gamemanager.params.GmRoleParam;
import com.xinyues.gamemanager.vo.VoGmRole;

@Service
public class GmRoleService {
    @Autowired
    private GmRoleRepository roleRepository;
    private AtomicLong seqId = new AtomicLong();

    @Autowired
    private GmMenuService menuService;

    public List<GmRole> getAllGmRole() {
        return roleRepository.findAll();
    }

    public GmRole getRole(String roleId) {
        return roleRepository.findById(roleId).orElse(null);
    }

    public void addGmRole(GmRoleParam addGmRoleParam) {
        GmRole gmRole = null;
        if (addGmRoleParam.getRoleId() != null) {
            gmRole = roleRepository.findById(addGmRoleParam.getRoleId()).orElse(null);
        }
        if (gmRole == null) {
            gmRole = new GmRole();
            gmRole.setId(StringUtil.uuid());
        }
        if (addGmRoleParam.getName() != null) {
            gmRole.setText(addGmRoleParam.getName());
        }
        // 获取此菜单下的所有的权限
        GmPermission gmPermission = gmRole.getChildren().stream().filter(p -> p.getId().equals(addGmRoleParam.getMenuId())).findFirst().orElse(null);
        if (gmPermission == null) {
            gmPermission = new GmPermission();
            gmPermission.setId(addGmRoleParam.getMenuId());
            gmRole.getChildren().add(gmPermission);
        }
        // 根据菜单id,查询此菜单下的所有的权限
        GmPermission nowPermission = menuService.getGmPermission(addGmRoleParam.getMenuId());
        if (nowPermission != null) {
            List<Permission> permissions = nowPermission.getChildren().stream().filter(p -> addGmRoleParam.getPermissionIds().contains(p.getId())).collect(Collectors.toList());
            gmPermission.getChildren().clear();// 删除旧的，保存新的
            gmPermission.getChildren().addAll(permissions);
        }
        roleRepository.save(gmRole);
    }

    public void deleteGmRole(String roleId) {
        this.roleRepository.deleteById(roleId);
    }


    public List<VoGmRole> getVoGmRole(List<GmRole> gmRoles) {
        List<VoGmRole> voGmRoles = new ArrayList<>();
        GmMenu menu = menuService.getGmMenu();
        gmRoles.forEach(r -> {
            VoGmRole root = new VoGmRole();
            root.setId(r.getId());
            root.setUuid(r.getId() + "_" + seqId.incrementAndGet());
            root.setText(r.getText());
            root.setRole(true);
            // root.setState("closed");
            if (r.getChildren().size() > 0) {
                List<VoGmRole> menuList = new ArrayList<>();
                r.getChildren().forEach(p -> {
                    MenuNode menuNode = menuService.getMenuNodeByMenuId(menu, p.getId());
                    if (menuNode != null) {
                        VoGmRole menuVo = new VoGmRole();
                        menuVo.setParentId(r.getId());
                        menuVo.setId(menuNode.getId());
                        menuVo.setUuid(menuNode.getId() + "_" + seqId.incrementAndGet());
                        menuVo.setText(menuNode.getText());
                        menuVo.setMenu(true);
                        // menuVo.setState("closed");
                        if (p.getChildren().size() > 0) {
                            List<VoGmRole> permissions = new ArrayList<>();
                            p.getChildren().forEach(permission -> {
                                VoGmRole permissionNode = new VoGmRole();
                                permissionNode.setUuid(permission.getId() + "_" + seqId.incrementAndGet());
                                permissionNode.setId(permission.getId());
                                permissionNode.setText(permission.getText());
                                permissionNode.setParentId(p.getId());
                                permissionNode.setHref(permission.getHref());
                                permissions.add(permissionNode);

                            });
                            menuVo.setChildren(permissions);
                        }
                        menuList.add(menuVo);
                    }
                });
                root.setChildren(menuList);
            }

            voGmRoles.add(root);
        });
        return voGmRoles;
    }



}
